package mesServlets;

import java.io.IOException;
import java.io.PrintWriter;

import javafx.beans.binding.SetBinding;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.javafx.runtime.async.BackgroundExecutor;

import pojoServlet.GestionCookie;
import sun.font.FontFamily;

/**
 * Servlet implementation class VerifierCookie
 */
@WebServlet(name = "verifierCookie", urlPatterns = { "/verifierCookie" })
public class VerifierCookie extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VerifierCookie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
		
	    //recupere le ookie qui a la clef "user"
		String monCookie = GestionCookie.getCookie(request, "user");
		
		out.println(
				
				"<!DOCTYPE html>"+
				"<html>"+
				"<head>"+
				"<meta charset=\"UTF-8\">"+
				"<title>Insert title here</title>"+
				"</head>"+
				"<body>"+					
					"<form action=\"verifierCookie\" method=\"post\" >"+						
						"<label>Nom</label>"+
						"<input type=\"text\" name=\"nomUser\"  value=\""+(monCookie != null ? monCookie : "")+"\" />"+	
						"<label>couleur preferee</label>"+
						"<input type=\"text\" name=\"couleur\" />"+	
						"<input type=\"submit\" value=\"OK\" />"+
					"</form>"+

				"</body>"+
				"</html>"		
				);
		
		out.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		//recuperer du formulaire le champs qui a l attribut name="nomUser"
		String nomUser = request.getParameter("nomUser");
		String couleur = request.getParameter("couleur");
		
		if (!nomUser.equals("")) {		
			//si le champs est pas vide je l ajoute au cookie
		
			out.println("bonjour"+"  "+nomUser);
			response.setContentType("text/html");
			out.println("<body bgcolor="+couleur+">");
			 
		} else {
			// si le champs est vide je supprime le cookie
			out.println("entrer un nom et une couleur");
			response.setContentType("text/html");
			doGet(request, response);

		}
	}
	}


